package com.web709.controller;

import com.web709.common.R;
import com.web709.entity.Carousel;
import com.web709.service.CarouselService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/carousel")
@Api(tags = "轮播图管理接口")
public class CarouselController {
    @Autowired
    private CarouselService carouselService;
    @PostMapping("/addcarousel")
    @ApiOperation(value = "添加轮播图", notes = "{\n" +
            "    \"title\":\"标题\",\n" +
            "    \"imgUrl\":\"https://web709.oss-cn-beijing.aliyuncs.com/2023-06-07/01.jpg\",\n" +
            "    \"content\":\"轮播图内容\"}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imgUrl", value = "轮播图地址", required = true),
            @ApiImplicitParam(name = "content", value = "轮播图内容", required = true),
            @ApiImplicitParam(name = "token", required = false,paramType = "header",dataType = "String")
    })
    public R<String> addcarousel(@RequestBody Carousel carousel){
        carouselService.save(carousel);
        return R.success("添加成功");
    }
    @DeleteMapping("/delcarousel")
    @ApiOperation(value = "删除轮播图", notes = "{\n" +
            "    \"id\":1\n" +
            "}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "轮播图id", required = true),
            @ApiImplicitParam(name = "token", required = false,paramType = "header",dataType = "String")
    })
    public R<String> delcarousel(Integer id){
        carouselService.removeById(id);
        return R.success("删除成功");
    }
    @PutMapping("/updatecarousel")
    @ApiOperation(value = "修改轮播图", notes = "{\n" +
            "    \"id\":1,\n" +
            "    \"imgUrl\":\"https://web709.oss-cn-beijing.aliyuncs.com/2023-06-07/01.jpg\",\n" +
            "    \"content\":\"轮播图内容\"}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "轮播图id", required = true),
            @ApiImplicitParam(name = "imgUrl", value = "轮播图地址", required = true),
            @ApiImplicitParam(name = "content", value = "轮播图内容", required = true),
            @ApiImplicitParam(name = "token", required = false,paramType = "header",dataType = "String")
    })
    public R<String> updatecarousel(@RequestBody Carousel carousel){
        carouselService.updateById(carousel);
        return R.success("修改成功");
    }
    @GetMapping("/getcarousel")
    @ApiOperation(value = "获取轮播图", notes = "获取轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", required = false,paramType = "header",dataType = "String")
    })
    public R<List<Carousel>> getCarousel() {
        try {
            List<Carousel> list = carouselService.list();
            if (list == null) {
                list = new ArrayList<>();
            }
            return R.success(list);
        } catch (Exception e) {
            return R.error("Failed to fetch carousel data");
        }
    }


}
